package com.baizhi.springboot.aspect;

import com.alibaba.fastjson.JSON;
import com.baizhi.springboot.annotation.LogAnnotation;
import com.baizhi.springboot.dao.LogDao;
import com.baizhi.springboot.entity.Admin;
import com.baizhi.springboot.entity.Log;
import com.baizhi.springboot.util.LogUtil;
import org.apache.activemq.command.ActiveMQQueue;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
//@Configurable
public class LogAspect {
    @Autowired
    private LogDao logDao;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @AfterReturning("@annotation(com.baizhi.springboot.annotation.LogAnnotation)")
    public void after(JoinPoint joinPoint) {
        Log log=new Log();
        //通过springboot提供的RequestContextHolder获取request
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        log.setLogDate(new Date());
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        log.setAdminId(admin.getId());
        //获得签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获得目标方法
        Method method = signature.getMethod();
        //获得目标方法的注解
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        //获得目标方法的注解content
        String content = annotation.content();
        String type = annotation.type();
        log.setLogContent(content);
        log.setLogType(type);

        //工具类获取真实IP
        String ipAddress = LogUtil.getIpAddr(request);
        log.setLogIp(ipAddress);
        //logDao.insertLog(log);
        //把日志对象转换成json串放入到MQ中
        String logString = JSON.toJSONString(log);
        //1.第一种方法MQ生产者
        jmsTemplate.convertAndSend(queue,logString);

        /*
        2.第二种方法
        jmsTemplate.send(queue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(logString);
            }
        });*/

    }

}
